<template>
	<s-layout title="发票管理" :bgStyle="{ color: '#FFF' }">
		<view v-if="state.list.length">
			<s-invoice-item v-for="item in state.list" hasBorderBottom :key="item.id" :item="item"
				:isDefault="item.is_default" @tap="onSelect(item)"></s-invoice-item>
		</view>

		<su-fixed bottom placeholder>
			<view class="footer-box ss-flex ss-row-between ss-p-20">
				<button class="add-btn ss-reset-button ui-Shadow-Main" @tap="handleAddInvoice">
					新增发票抬头
				</button>
			</view>
		</su-fixed>
		<s-empty v-if="state.list.length === 0 && !state.loading" text="暂无发票" icon="/static/data-empty.png" />
	</s-layout>
</template>

<script setup>
	import { reactive } from 'vue';
	import { onShow,onLoad } from '@dcloudio/uni-app';
	import sheep from '@/sheep';
	import _ from 'lodash';
	const state = reactive({
		list: [],
		loading: true,
		invoice_amount: '',
		orderData: null // 存储订单数据
	});
	const handleAddInvoice = () => {
		// 传递订单数据到编辑页面
		const params = {
			invoice_amount: state.invoice_amount
		};
		
		if (state.orderData) {
			params.orderData = JSON.stringify(state.orderData);
		}
		
		sheep.$router.go('/pages/user/invoice/edit', params);
	}

	const onSelect = (invoiceInfo) => {
		uni.$emit('SELECT_INVOICE', {
			invoiceInfo,
			invoice_amount: state.invoice_amount,
			orderData: state.orderData // 传递订单数据
		});
		sheep.$router.back();
	};
	onLoad((option) => {
		state.invoice_amount = option.invoice_amount;
		
		// 接收订单数据
		if (option.orderData) {
			state.orderData = JSON.parse(option.orderData);
			console.log('接收到的订单数据:', state.orderData);
		}
	})
	onShow(async () => {
		state.list = (await sheep.$api.user.invoice.list()).data;
		state.loading = false;
	});
</script>

<style lang="scss" scoped>
	// page{
	//   background-color: red;
	// }
	.footer-box {
		.add-btn {
			flex: 1;
			background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
			border-radius: 8rpx;
			font-size: 30rpx;
			font-weight: 500;
			line-height: 80rpx;
			color: $white;
			position: relative;
			z-index: 1;
		}

		.sync-wxaddress {
			flex: 1;
			line-height: 80rpx;
			background: $white;
			border-radius: 8rpx;
			font-size: 30rpx;
			font-weight: 500;
			color: $dark-6;
			margin-right: 16rpx;
		}
	}
</style>